package com.share.example.classload.main;

import com.share.common.constant.DirConstant;
import com.share.common.utils.EncryptUtil;
import com.share.common.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

@Slf4j
public class EncrypteClass {
    private static String encrptyPackage = "com.share.example.classload.service.impl";

    public static void main(String[] args) {
        File classFileHome = new File(EncrypteClass.class.getResource("/").getPath());
        int dirPathHomeLen = classFileHome.getAbsolutePath().length();
        List<File> files = FileUtil.listFile(classFileHome);
        for (File file : files) {
            String classFileName = file.getAbsolutePath().substring(dirPathHomeLen + 1);
            int index = classFileName.lastIndexOf(File.separator);
            String fileName = classFileName.substring(index + 1);
            String packageName = classFileName.substring(0, index).replaceAll(File.separator + File.separator, ".");

            //对特定的文件进行加密
            if (packageName.startsWith(encrptyPackage)) {
                byte[] bytes = FileUtil.readByte(file);
                byte[] encrypt = EncryptUtil.encrypt(bytes);
                String outputFilePath = DirConstant.PROJECT_ROOT_HOME + DirConstant.CLAZZ_DIR + File.separator + fileName;
                FileUtil.writeByte(outputFilePath, encrypt);
            }
        }

    }
}
